home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1996 Fall / BMUG Fall'96 PD-ROM.iso / Education / Math / MathPad 2.4 / Examples / regression < prev    next >
Text File  |  1996-03-30  |  913b  |  38 lines

  1. -- "linear regression" fit x,y data to a line
  2. -- See Also: XFun "linfit"
  3. -- uses := to avoid re-calculating sums
  4. sumx := sum(x[i],i,1,n):
  5. sumxx:= sum(x[i]^2,i,1,n):
  6. sumy := sum(y[i],i,1,n):
  7. sumyy:= sum(y[i]^2,i,1,n):
  8. sumxy:= sum(x[i]*y[i],i,1,n):
  9.  
  10. Sxx=n*sumxx-sumx^2
  11. Sxy=n*sumxy-sumx*sumy
  12.  
  13. slope=Sxy/Sxx
  14. intercept=(sumxx*sumy-sumx*sumxy)/(n*sumxx-sumx^2)
  15. correlation=Sxy/sqrt(Sxx*(n*sumyy-sumy^2))
  16.  
  17. -- user must supply a data file of x y values.
  18. data=read("xydata")
  19. n=count(data)   -- number of data points
  20.  
  21. -- file is organized as x,y pairs. 
  22. x[i]=data[i,1] dim[n] -- Make sep x and y arrays
  23. y[i]=data[i,2] dim[n]
  24.  
  25. plot data                -- plot data points
  26. plot X*slope+intercept   -- plot best fit line
  27.  
  28. label slope:0.720
  29. label intercept:-1.122
  30. label correlation:0.973
  31.  
  32. -- Note: can also be used to do 2 param fit for other functions.
  33. ~
  34.  f(x) = -cos(x)
  35.  plot f(X)*slope+intercept
  36.  x[i]=f(data[i,1]) dim[n]
  37. ~
  38.